//
//  StatsViewController.m
//  iOSUI
//
//  Created by Zhao Yun on 12/26/13.
//  Copyright (c) 2013 ainemo. All rights reserved.
//

#import "StatsViewController.h"

#define DBA_ARRAY_SIZE  100
@interface StatsViewController (){
    NSTimer *_timer;
    NSMutableArray *_DBAInfo;
    NSDate *_date;
}

@end

@implementation StatsViewController

@synthesize statsSegment;

- (void)statisticsTimer:(NSTimer *)timer
{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeEclips = [currentDate timeIntervalSinceDate:_date];
    if (timeEclips < 0.8f) {
        return;
    }
    else {
        _date = [NSDate date];
    }
    
    NSString *dbaStatistics = [[XYSDKManager sharedInstance] getDBStatistics];
    [_DBAInfo addObject:dbaStatistics];
    
    while (_DBAInfo.count>DBA_ARRAY_SIZE)
    {
        [_DBAInfo removeObjectAtIndex:0];
    }
        
    NSString *info = @"";
    if (statsSegment.selectedSegmentIndex == 0)
    {
        info = [[XYSDKManager sharedInstance] getVideoTxStatistics];
    }
    else if (statsSegment.selectedSegmentIndex == 1)
    {
        info = [[XYSDKManager sharedInstance] getVideoRxStatistics];
    }
    else if (statsSegment.selectedSegmentIndex == 2)
    {
        info = [[XYSDKManager sharedInstance] getAudioTxStatistics];
    }
    else if (statsSegment.selectedSegmentIndex == 3)
    {
        info = [[XYSDKManager sharedInstance] getAudioRxStatistics];
    }
    else if (statsSegment.selectedSegmentIndex == 4)
    {
        info = [[XYSDKManager sharedInstance] getOtherStatistics];
    }
    else if (statsSegment.selectedSegmentIndex == 5)
    {
        for (NSUInteger i=0; i<[_DBAInfo count]; i++) {
            info = [info stringByAppendingString:[_DBAInfo objectAtIndex:i]];
        }
    }
    else if (statsSegment.selectedSegmentIndex == 6)
    {
        info = [[XYSDKManager sharedInstance] getLayoutStatistics];
        
//        NSLog(@"statisticsTimer info - %@",info);
    }
    
    CGPoint offset = _scrollView.contentOffset;
    CGRect frame = _labelInfo.frame;
    
    CGSize size = [info boundingRectWithSize:CGSizeMake(_labelInfo.frame.size.width, 3000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_labelInfo.font} context:nil].size;
    frame.size.height = size.height;
    _labelInfo.frame = frame;
    
    if (info.length>0) {
        NSMutableString *a = [NSMutableString stringWithString:info];
        NSRange range = [a rangeOfString:@"\n"];
        range = [a rangeOfString:@"\r"];
        range = [a rangeOfString:@"\r\n"];
        range = [a rangeOfString:@"\n\r"];
        [a replaceOccurrencesOfString:@"\r\n" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, a.length-1)];
        _labelInfo.text = a;
    }
    
    _scrollView.contentSize = frame.size;
    _scrollView.contentOffset = offset;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    if ([_timer isValid])
        [_timer invalidate];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(statisticsTimer:) userInfo:nil repeats:YES];
    _DBAInfo = [NSMutableArray arrayWithCapacity:DBA_ARRAY_SIZE];
    _date = [NSDate date];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if ([_timer isValid])
        [_timer invalidate];
}

-(IBAction)exit:(id)sender
{
    [_timer invalidate];
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}

@end
